#include <GL/glut.h>
#include <cmath>
#include "../zone.h"
#include "battleship.h"
#include "../../view/draw.h"
#include "scene/Objects.h"


void BattleShip::drawMissile(float seaLevel,int mode)
{
	glPushMatrix();
	glTranslatef(initialPosition.first,initialPosition.second,seaLevel);
	glRotatef(initialOrientation,0,0,1);
	glRotatef(270,1,0,0);
	glScalef(0.7,0.7,0.7);


	if(mode)
	{
		glColor3f(0.8,0.8,0.2);
		GLUquadric *cylindrQuad=gluNewQuadric();
		glPushMatrix();
		glTranslatef(0,0,-3.8);
		glScalef(0.5,0.5,1);
		glRotatef(90,1,0,0);
		glTranslatef(0,0,1);
		gluDisk(cylindrQuad,18,21,32,32);
		glPopMatrix();
	}


	GLUquadric *cylindrQuad=gluNewQuadric();

	glColor3f(0.1,0.1,0.1);
	gluDisk(cylindrQuad,0,4,16,16);
	gluCylinder(cylindrQuad,4,1,5,16,16);

	glTranslatef(0,0,5);
	gluDisk(cylindrQuad,0,1,16,16);


	if(player==1)
		glColor3f(0.6,0.2,0.2);
	else
		glColor3f(0.2,0.2,0.6);
	glTranslatef(0,0,-15);
	gluDisk(cylindrQuad,0,2.5,16,16);
	gluCylinder(cylindrQuad,2.5,2.5,10,16,16);

	glPopMatrix();
}


void BattleShip::drawMine(float seaLevel,int mode)
{
	if(player==1)
		glColor3f(0.6,0.2,0.2);
	else
		glColor3f(0.2,0.2,0.6);

	glPushMatrix();

	glTranslatef(initialPosition.first,initialPosition.second,seaLevel);
	glutSolidSphere(5,16,16);
	glTranslatef(0,0,5);
	glutSolidSphere(1.5,16,16);

	if(mode)
	{
		glColor3f(0.8,0.8,0.2);
		GLUquadric *cylindrQuad=gluNewQuadric();
		glPushMatrix();
		glTranslatef(0,0,-3.9);
		glScalef(0.5,0.5,1);
		gluDisk(cylindrQuad,18,21,32,32);
		glPopMatrix();
	}


	glPopMatrix();
}



void BattleShip::drawTradeShip(float seaLevel,int mode,int isDay)
{
	float spotlightPosition[]={0,0,0,1};
	float spotlightDirection[]={0,0,-1};

	float selectedspotlightPosition[]={0,0,0,1};
	float selectedspotlightDirection[]={0.7,0,-1};

	glPushMatrix();
	glTranslatef(initialPosition.first,initialPosition.second,seaLevel-2);
	glRotatef(initialOrientation+90,0,0,1);
	//glRotatef(90,1,0,0);
	glTranslatef(10,0,65);
	//glutSolidCube(10);
	if(isDay==NIGHT)
	{
		glEnable(GL_LIGHT3);
		glLightfv(GL_LIGHT3, GL_POSITION, spotlightPosition);
		glLightfv(GL_LIGHT3,GL_SPOT_DIRECTION, spotlightDirection);
	}
	if(mode && isDay==NIGHT)
	{
		glEnable(GL_LIGHT5);
		glLightfv(GL_LIGHT5, GL_POSITION, selectedspotlightPosition);
		glLightfv(GL_LIGHT5,GL_SPOT_DIRECTION, selectedspotlightDirection);
	}
	glPopMatrix();

	Objects::tradeship[player-1].SetPosition(initialPosition.first,initialPosition.second,seaLevel-2);
	Objects::tradeship[player-1].Rotate(90,initialOrientation-90,0);
	Objects::tradeship[player-1].Render();

	glPushMatrix();

	glTranslatef(initialPosition.first,initialPosition.second,seaLevel-2);
	glRotatef(initialOrientation+90,0,0,1);

	if(mode)
	{
		glColor3f(0.8,0.8,0.2);
		GLUquadric *cylindrQuad=gluNewQuadric();
		glPushMatrix();
		glTranslatef(9,0,5);
		glScalef(1.1,0.5,1);
		gluDisk(cylindrQuad,18,21,32,32);
		glPopMatrix();
	}

	glPopMatrix();
	if(isDay==NIGHT)
		glDisable(GL_LIGHT3);
}

BattleShip::BattleShip() {
	// TODO Auto-generated constructor stub
	isHigh=0;
	portPosition.first=0;
	portPosition.second=0;
	portOrientation=0;
	speed=0;

}

BattleShip::~BattleShip() {
	// TODO Auto-generated destructor stub
}

int BattleShip::getPlayer() const
{
   return player;
}

void BattleShip::setPlayer(int player)
{
   this->player = player;
}

pair<float,float> BattleShip::getInitialPosition() const
{
   return initialPosition;
}

void BattleShip::setInitialPosition(pair<float,float> position)
{
   initialPosition=position;
}

float BattleShip::getInitialOrientation() const
{
   return initialOrientation;
}

void BattleShip::setInitialOrientation(float orientation)
{
   initialOrientation=orientation;
}

pair<float,float> BattleShip::getPortPosition() const
{
   return portPosition;
}

void BattleShip::setPortPosition(pair<float,float> position)
{
   portPosition=position;
}

float BattleShip::getPortOrientation() const
{
   return portOrientation;
}

void BattleShip::setPortOrientation(float orientation)
{
   portOrientation=orientation;
}

bool BattleShip::isStatic()
{
	return (speed==0);
}

void BattleShip::calculatePosition()
{
	if(speed<0.15 && speed>-0.15)
		speed=0;
	else if(speed>0)
		speed-=0.25;
	else if(speed<0)
		speed+=0.25;

	initialPosition.first+=speed*cos((initialOrientation+90.0)* M_PI /180.0);
	initialPosition.second+=speed*sin((initialOrientation+90.0)* M_PI /180.0);
}

void BattleShip::move(int mode)
{
	if(mode==GLUT_KEY_DOWN)
	{
		if(speed>=-6.5)
			speed-=0.45;
	}
	else if(mode==GLUT_KEY_UP)
	{
		if(speed<=6.5)
			speed+=0.45;
	}
	else if(mode==GLUT_KEY_LEFT)
		initialOrientation+=5.5;
	else if(mode==GLUT_KEY_RIGHT)
		initialOrientation-=5.5;
}

void BattleShip::draw3d(float seaLevel,int mode,int isDay)
{


	float spotlightPosition[]={0,0,0,1};
	float spotlightDirection[]={0,0,-1};

	float selectedspotlightPosition[]={0,0,0,1};
	float selectedspotlightDirection[]={0.7,0,-1};

	glPushMatrix();
	glTranslatef(initialPosition.first,initialPosition.second,seaLevel-2);
	glRotatef(initialOrientation+90,0,0,1);
	glTranslatef(20,0,70);
	//glutSolidCube(10);
	if(isDay==NIGHT)
	{
		glEnable(GL_LIGHT4);
		glLightfv(GL_LIGHT4, GL_POSITION, spotlightPosition);
		glLightfv(GL_LIGHT4,GL_SPOT_DIRECTION, spotlightDirection);
	}
	if(mode && isDay==NIGHT)
	{
		glLightfv(GL_LIGHT5, GL_POSITION, selectedspotlightPosition);
		glLightfv(GL_LIGHT5,GL_SPOT_DIRECTION, selectedspotlightDirection);
	}
	glPopMatrix();

	glPushMatrix();

	glTranslatef(initialPosition.first,initialPosition.second,seaLevel-2);
	glRotatef(initialOrientation+90,0,0,1);

	if(mode)
	{
		glColor3f(0.8,0.8,0.2);
		GLUquadric *cylindrQuad=gluNewQuadric();
		glPushMatrix();
		glTranslatef(-1,0,5);
		glScalef(2.2,0.9,1);
		gluDisk(cylindrQuad,18,21,32,32);
		glPopMatrix();
	}

	glPopMatrix();


	Objects::battleship[player-1].SetPosition(initialPosition.first,initialPosition.second,seaLevel+1.4);
	Objects::battleship[player-1].Rotate(90,initialOrientation,0);
	Objects::battleship[player-1].Render();

	if(isDay==NIGHT)
		glDisable(GL_LIGHT4);

}
